package cn.lsnu.heritage.service.impl;

import cn.lsnu.heritage.mapper.CategoryMapper;
import cn.lsnu.heritage.mapper.IcHeritageMapper;
import cn.lsnu.heritage.model.domain.Category;
import cn.lsnu.heritage.model.domain.IcHeritage;
import cn.lsnu.heritage.model.dto.IcHeritageDTO;
import cn.lsnu.heritage.model.dto.Top9Dto;
import cn.lsnu.heritage.model.vo.IcHeritageVO;
import cn.lsnu.heritage.service.IcHeritageService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author LindaMan
 * @description 针对表【ic_heritage(非遗表)】的数据库操作Service实现
 * @createDate 2023-01-16 21:43:57
 */
@Service
public class IcHeritageServiceImpl extends ServiceImpl<IcHeritageMapper, IcHeritage>
        implements IcHeritageService {
    @Resource
    private IcHeritageMapper icHeritageMapper;
    @Resource
    private CategoryMapper categoryMapper;


    /**
     * 根据分类id查询非遗列表
     * @param id
     * @return
     */
    @Override
    public List<IcHeritageDTO> queryHeritageListByCategoryId(Integer id) {
        List<IcHeritage> heritageList;
        //0.判断id 查询全部还是查询分类
        if (id == 0){
            // 查询全部
            heritageList = icHeritageMapper.selectList(null);
        }else{
            //查询分类
            //1.构造查询条件
            LambdaQueryWrapper<IcHeritage> queryWrapper = Wrappers.lambdaQuery(IcHeritage.class).eq(IcHeritage::getCategoryId, id);

            //2.查询非遗列表
            heritageList = icHeritageMapper.selectList(queryWrapper);
        }

        //3.封装返回对象
        List<IcHeritageDTO> heritageDTOList = Optional.ofNullable(heritageList.stream().map(h ->
                convertHeritageToDTO(h, categoryMapper)
        ).collect(Collectors.toList())).orElse(new ArrayList<IcHeritageDTO>());

        return heritageDTOList;
    }

    /**
     * 根据条件查询非遗集合 （模糊查询）
     *
     * @param heritageVO
     * @return
     */
    @Override
    public Page<IcHeritageDTO> queryHeritageList(IcHeritageVO heritageVO) {
        //参数预处理
        heritageVO.setPageNum((heritageVO.getPageNum() - 1) * heritageVO.getPageSize());

        //查询categoryId
        List<Integer> categoryIdList = heritageVO.getCategoryList().stream().map(name -> {
            LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Category::getName,name);
            Category category = categoryMapper.selectOne(queryWrapper);
            return category.getId();
        }).collect(Collectors.toList());
        heritageVO.setCategoryIdList(categoryIdList);

        //模糊查询
        List<IcHeritage> icHeritageList = icHeritageMapper.selectHeritageList(heritageVO);

        //封装Page<IcHeritageDTO>
        List<IcHeritageDTO> tempList = icHeritageList.stream().map(heritage -> convertHeritageToDTO(heritage, categoryMapper)).collect(Collectors.toList());

        //封装records
        Page<IcHeritageDTO> heritageDTOPage = new Page<>();
        heritageDTOPage.setTotal(icHeritageMapper.selectCountOfHeritageList(heritageVO));
        heritageDTOPage.setRecords(tempList);

        return heritageDTOPage;
    }

    @Override
    public List<IcHeritageDTO> queryHotHeritageList() {
        List<IcHeritage> heritages = icHeritageMapper.queryHotHeritageList();
        List<IcHeritageDTO> heritageDTOList = heritages.stream().map(heritage ->
                convertHeritageToDTO(heritage, categoryMapper)
        ).collect(Collectors.toList());

        return heritageDTOList;
    }

    @Override
    public IcHeritageDTO queryHeritageById(Integer id) {
        IcHeritage heritage = icHeritageMapper.selectById(id);

        return convertHeritageToDTO(heritage, categoryMapper);
    }

    @Override
    public List<Top9Dto> queryHotCategoryList() {
        List<Top9Dto> list = null;
        try {
            list = icHeritageMapper.select_top();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    private static IcHeritageDTO convertHeritageToDTO(IcHeritage heritage, CategoryMapper categoryMapper) {
        IcHeritageDTO dto = new IcHeritageDTO();
        //对象复制
        BeanUtils.copyProperties(heritage, dto);
        Category category = categoryMapper.selectById(heritage.getCategoryId());
        dto.setCategoryName(category.getName());
        dto.setCategoryImage(category.getImage());
        return dto;
    }
}




